package com.erp.module.yiJianXiang

import android.content.Context
import android.util.Log
import com.erp.bean.TouSuBean
import com.erp.bean.TouSuSouSuoBean
import com.erp.Interface.OnUserDataInterface
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2019/3/20/020.
 * 作者：刘洋
 */
class IYiJianXiangP(var  V: YiJianXiangContract.YiJianXiangView, var  context : Context, var list :MutableList<TouSuBean>): BasePullP(),YiJianXiangContract.YiJianXiangP {


    var md  = YiJianXiangModel()
    var sousuobean = TouSuSouSuoBean()
    init {
        PullV = V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.getTouSu(context, page, pagesize,sousuobean,onUserDataInterface)
    }

    override fun delete(pos: Int) {
        md.deleteTouSu(context,list[pos].id,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                list.removeAt(pos)
                V.deleteOk()
            }

            override fun onNot(throwable: String?) {
                V.deleteNo()
            }

        })
    }

    override fun jsondata(data: String?) {
        if(data==null){
            return
        }
        try {
            val js = JSONArray(data)
            for (i in 0 until js.length()) {
                val bean = TouSuBean()
                val jo = js.get(i) as JSONObject
                bean.id = JsonUtil.getvolue(jo, "id")
                bean.chuangqu = JsonUtil.getvolue(jo, "han_qu")
                bean.bumen = JsonUtil.getvolue(jo, "bu_men")
                bean.tousu = JsonUtil.getvolue(jo, "complaint")
                bean.jianyi = JsonUtil.getvolue(jo, "suggestion")
                bean.ctime = JsonUtil.getvolue(jo, "ctime")
                bean.reply = JsonUtil.getvolue(jo, "reply")
                bean.creator_name = JsonUtil.getvolue(jo, "creator_name")
                bean.duty_bumen = JsonUtil.getvolue(jo, "duty_bumen")
                bean.duty_name = JsonUtil.getvolue(jo, "duty_name")
                val jsp = JsonUtil.getjsvolue(jo, "files")
                if (jsp != null) {
                    for (j in 0 until jsp.length()) {
                        val jop = jsp.get(j) as JSONObject
                        val photo = TouSuBean.photo()
                        photo.fid = JsonUtil.getvolue(jop, "fid")
                        photo.filename = JsonUtil.getvolue(jop, "filename")
                        photo.uri = JsonUtil.getvolue(jop, "uri")
                        bean.photoList.add(photo)
                    }
                }
                list.add(bean)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    override fun listClean() {
        list.clear()
    }

    override fun sousuo(sousuobean: TouSuSouSuoBean) {
        this.sousuobean=sousuobean
        xiala()
    }
}